package cn.chenmanman.system.controller;

import cn.chenmanman.common.result.Result;
import cn.chenmanman.model.system.SysRole;
import cn.chenmanman.model.system.SysUser;
import cn.chenmanman.model.vo.SysUserQueryVo;
import cn.chenmanman.system.mapper.SysUserRoleMapper;
import cn.chenmanman.system.service.SysUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "用户管理接口 ")
@RestController
@RequestMapping("/admin/system/user")
public class SysUserController {
    @Autowired
    private SysUserService userService;



    /**
     * 分页查询
     * */
    @ApiOperation("用户列表")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       SysUserQueryVo sysUserQueryVo){
        Page<SysUser> sysUserIpage = new Page<SysUser>(page, limit);
        IPage<SysUser> userIPage = userService.selectPage(sysUserIpage, sysUserQueryVo);
        return Result.ok(userIPage);
    }

    @ApiOperation("新增用户")
    @PostMapping()
    public Result addUser(@RequestBody SysUser sysUser){
        boolean is_success = userService.save(sysUser);
        return is_success?Result.ok():Result.fail();
    }

    @ApiOperation("修改用户")
    @PostMapping("update")
    public Result updateUser(@RequestBody SysUser sysUser){
        boolean is_success = userService.updateById(sysUser);
        return is_success?Result.ok():Result.fail();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public Result removeUser(@PathVariable("id") Long id){
        boolean is_success = userService.removeById(id);
        return is_success?Result.ok():Result.fail();
    }


    @ApiOperation("批量删除用户")
    @DeleteMapping("deleteBatch")
    public Result batchRemoveUser(@RequestBody List<Long> ids){
        boolean b = userService.removeByIds(ids);
        return b?Result.ok():Result.fail();
    }

    @ApiOperation("根据id获取用户")
    @GetMapping("{id}")
    public Result getUser(@PathVariable("id") Long id){
        SysUser user = userService.getById(id);
        return Result.ok(user);
    }

    @ApiOperation("禁用用户")
    @GetMapping("userDisable")
    public Result userDisable(Integer id, Integer status) {
        userService.userDisable(id, status);
        return Result.ok();
    }


}
